package com.marketManage;

import java.text.SimpleDateFormat;
import java.util.*;

public class MarketMag {
	List<Member> list = new ArrayList<>();

	// 3、增加积分
	public Member addIntegral(int cardNum, String Password, int money) {
		Member m = checkID2(cardNum, Password);
		if (m != null) {
			m.setIntegral(m.getIntegral() + money);
		}
		return m;

	}

	// 4、积分兑换
	public Member exIntegral(int cardNum, String Password, int integral) {
		Member m = checkID2(cardNum, Password);
		if (m != null) {
			if (integral % 100 != 0) {
				System.out.println("兑换的积分必须是100的倍数！");
				return null;
			} else {
				if (integral <= m.getIntegral()) {
					m.setIntegral(m.getIntegral() - integral);
				} else {
					System.out.println("兑换的积分不得高于自己的积分！");
					return null;
				}
			}
		}
		return m;
	}

	// 5、查询积分
	public Member showintegral(int cardNum, String Password) {
		Member m = checkID2(cardNum, Password);
		return m;
	}

	// 寻找卡号在list中的位置
	public int findMember(int cardNum) {
		for (int i = 0; i < list.size(); i++) {
			if (cardNum == list.get(i).getCardNumber()) {
				return i;
			}
		}
		return -1;
	}

	// 1检查账号密码是否正确,返回Boolean
	public boolean checkID(int cardNum, String Password) {
		int num = findMember(cardNum);
		if (Password.equals(list.get(num).getPassword())) {
			return true;
		}
		return false;
	}

	// 2检查账号密码是否正确，返回对象
	public Member checkID2(int cardNum, String Password) {
		if (list.get(findMember(cardNum)).getPassword().equals(Password)) {
			return list.get(findMember(cardNum));
		}
		return null;
	}

	// 3检查账号密码是否正确，返回对象
	public Member checkID3(int cardNum, String Password) {
		for (Member m : list) {
			if (m.getCardNumber() == cardNum && m.getPassword().equals(Password)) {
				return m;
			}
		}
		return null;
	}

	// 2、修改密码
	public boolean changePassword(int cardNum, String oldPassword, String newPassword) {
		if (checkID2(cardNum, oldPassword) == null) {
			System.out.println("您输入的卡号或者密码错误！");
			return false;
		} else {
			checkID2(cardNum, oldPassword).setPassword(newPassword);
			return true;
		}

	}

	// 检查密码位数
	public boolean cheakPassword(String pw) {
		if (pw.length() < 6) {
			System.out.print("会员密码不能小于6位，您输入的是" + pw + "请重新输入注册密码：");
			return false;
		}
		// return pw.length() >= 6;
		return true;
	}

	// 得到时间
	public String getDate(Date d) {
		Date d2 = new Date();
		if (d.getYear() == d2.getYear()) {
			SimpleDateFormat s1 = new SimpleDateFormat("MM月dd日");
			return s1.format(d);

		} else {
			SimpleDateFormat s2 = new SimpleDateFormat("yy年MM月dd日");
			return s2.format(d);
		}
	}

	// 1、注册新用户
	public boolean openCard(String name, String password) {
		int cardNum = (int) (10000000 + Math.random() * 90000000);
		Date d = new Date();
		list.add(new Member(name, cardNum, password, 100, d));
		// System.out.println(list);
		return true;
	}

}
